import Vue from 'vue'
import VueRouter from 'vue-router'
import HomePage from '../views/HomePage.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: "/home"
  },
  {
    path: '/home',//首页
    name: 'home',
    component: HomePage,

  },
  {
    path: '/sub',//子页面
    name: 'sub',
    component: () => import('../views/SubPages.vue'),

  },
  {
    path: '/detailpage',//大图详情页
    name: 'detailpage',
    component: () => import('../views/DetailPage.vue'),
  },
  {
    path: '/shoppingcar',//购物车
    name: 'shoppingcar',
    component: () => import('../views/ShoppingCart.vue'),
  },
  // {
  //   path: '/personal',//个人中心
  //   name: 'personal',
  //   component: () => import('../views/PersonalCenter.vue'),
  // },
  {
    path: '/bannerpage',//轮播图页面
    name: 'bannerpage',
    component: () => import('../views/bannerPage.vue'),
  },
  {
    path: '/allchannels',//轮播图页面
    name: 'allchannels',
    component: () => import('../views/AllChannels.vue'),
  },
  {
    path: '/login',//登录页
    name: 'login',
    component: () => import('../views/LoginView.vue'),
  },
  {
    path: '/cookbook',//菜谱
    name: 'cookbook',
    component: () => import('../views/CookBook.vue'),
  },
  {
    path: '/delicacies',//制作方法
    name: 'delicacies',
    component: () => import('../views/DelicaciesView.vue'),
  },
  {
    path: '/personal',//登陆后的页面
    name: 'personal',
    component: () => import('../views/PersonalCenter.vue'),
    // 独享路由守卫
    beforeEnter: (to, from, next) => {
      let token = window.localStorage.getItem("token");
      // 判断是否登录
      if (!token) {
        next("/login");
        return;
      } else {
        next();
      }
    }
  },


]



// 注意:解决路由跳转问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
}


const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
  scrollBehavior: function () {
    return { x: 0, y: 0 }
  }
})

export default router
